import 'dart:convert' show json;
import 'package:loxen/mode/material/material_order_mode.dart';
import 'package:loxen/utils/time_util.dart';

class OrderList {
  OrderList({
    this.count,
    this.next,
    this.previous,
    this.results,
  });

  factory OrderList.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<Order> results = jsonRes['results'] is List ? <Order>[] : null;
    if (results != null) {
      for (final dynamic item in jsonRes['results']) {
        if (item != null) {
          results.add(Order.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return OrderList(
      count: asT<int>(jsonRes['count']),
      next: asT<String>(jsonRes['next']),
      previous: asT<String>(jsonRes['previous']),
      results: results,
    );
  }

  int count;
  String next;
  String previous;
  List<Order> results;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'count': count,
        'next': next,
        'previous': previous,
        'results': results,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Order {
  Order({
    this.id,
    this.mode,
    this.status,
    this.product,
    this.startTime,
    this.endTime,
    this.brand,
    this.serviceCenter,
  });

  bool isCancel() {
    if (status == '7' || status == '8' || status == '9') {
      return false;
    } else {
      return true;
    }
  }

  String getMode() {
    if (mode == '1') {
      return '同城送样';
    }
    if (mode == '2') {
      return '到店取样';
    }
    if (mode == '3') {
      return '快递寄送';
    }
    return '未知状态';
  }

  String getState() {
    if (status == '1') {
      return "等待服务中心确认";
    }
    if (status == '2') {
      return "等待服务中心备样";
    }

    if (status == '3') {
      return "等待业务员送样";
    }

    if (status == '4') {
      return "等待用户取样";
    }
    if (status == '5') {
      return "服务中心发样中";
    }

    if (status == '6') {
      return "等待用户收样";
    }

    if (status == '7') {
      return "已收样";
    }

    if (status == '8') {
      return "已完成";
    }

    if (status == '9') {
      return "已关闭";
    }
    return '未知状态';
  }

  factory Order.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }
    final List<Product> product = jsonRes['product'] is List ? <Product>[] : null;
    if (product != null) {
      for (final dynamic item in jsonRes['product']) {
        product.add(Product.fromJson(asT<Map<String, dynamic>>(item)));
      }
    }
    return Order(
      id: asT<int>(jsonRes['id']),
      mode: asT<String>(jsonRes['mode']),
      status: asT<String>(jsonRes['status']),
      product: product,
      startTime: asT<String>(jsonRes['delivery_start_time']),
      endTime: asT<String>(jsonRes['delivery_end_time']),
      brand: Brand.fromJson(asT<Map<String, dynamic>>(jsonRes['brand'])),
      serviceCenter: ServiceCenter.fromJson(asT<Map<String, dynamic>>(jsonRes['service_centre']))
    );
  }

  int id;
  String mode;
  String status;
  List<Product> product;
  String startTime;
  String endTime;
  Brand brand;
  ServiceCenter serviceCenter;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'mode': mode,
        'status': status,
        'product': product,
        'delivery_start_time': startTime,
        'delivery_end_time': endTime,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class ServiceCenter {
  ServiceCenter({
    this.id,
    this.name,
    this.address,
    this.lng,
    this.lat,
    this.mobile,
  });

  factory ServiceCenter.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : ServiceCenter(
          id: asT<int>(jsonRes['id']),
          name: asT<String>(jsonRes['name']),
          address: asT<String>(jsonRes['address']),
          lng: asT<String>(jsonRes['lng']),
          lat: asT<String>(jsonRes['lat']),
          mobile: asT<String>(jsonRes['mobile']),
        );

  int id;
  String name;
  String address;
  String lng;
  String lat;
  String mobile;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'name': name,
        'address': address,
        'lng': lng,
        'lat': lat,
        'mobile': mobile,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
